<div class="row content-nav">
    <div class="col-xs-12">
        <ul>
            <li><a href="javascript:;"><i></i>&nbsp;会员管理</a></li>
            <li><a href="javascript:;">会员列表</a></li>
        </ul>
    </div>
</div>

<div class="layui-fluid" id="search-box">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane" action="">

                <div class="layui-form-item">

                    <div class="layui-inline">
                        <label class="layui-form-label seller-inline-2">收服务费：</label>
                        <div class="layui-input-inline seller-inline-4">
                            <select name="has_service_charge">
                                <option value="-1">全部</option>
                                <option value="1">是</option>
                                <option value="2">否</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label seller-inline-2">余额状态：</label>
                        <div class="layui-input-inline seller-inline-4">
                            <select name="balance">
                                <option value="-1">全部</option>
                                <option value="1"><= 20 USDT</option>
                                <option value="2"> > 20 USDT</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label seller-inline-2">用户名：</label>
                        <div class="layui-input-inline seller-inline-4">
                            <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <button type="button" id="search-btn" class="layui-btn layui-btn-sm layui-bg-light-blue"><i class="layui-icon layui-icon-search"></i> 搜索</button>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>

<section class="content clearfix">
    <div class="layui-list-box">
        <table class="layui-hide" id="list-box" lay-filter="list-box"></table>
    </div>
</section>

<script type="text/html" id="list-toolbar">
    <div class="layui-btn-container">
 <!--       <button lay-event="url" type="button" class="layui-btn layui-btn-sm layui-bg-light-blue"><i class="layui-icon">&#xe61f;</i> 添加</button>-->
        <button lay-event="refresh" type="button" class="layui-btn layui-btn-sm layui-bg-red"><i class="layui-icon">&#xe9aa;</i> 刷新</button>
    </div>
</script>

<script type="text/html" id="list-bar">
   <!-- <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="finance">财务</a>-->
    <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="recharge">充值</a>
    <!--<a class="layui-btn layui-btn-xs layui-bg-cyan" lay-event="tags">提现</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="withdrawal">提现</a>
   <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">VIP</a>-->
</script>

<script>

    layui.use(['table','form'], function () {
        var table = layui.table;
        var form = layui.form;

        table.render({
            elem: '#list-box'
            , url: '{:createUrl("index")}'
            , toolbar: '#list-toolbar'
            , defaultToolbar: []
            , title: '数据表'
            , cols: [[
                {type: 'checkbox'}
                , {field: 'id', title: 'id',minWidth:80}
                , {field: 'username', minWidth:150,title: '用户名'}
                , {field:'', title:'VIP 期限', minWidth:110,align:"center",templet: function(res){
                        if(!res.vip) {
                            return '<tag class="layui-btn layui-bg-gray layui-btn-xs" lay-event="setVip">否</tag>';
                        }else if(!res.vip_expire) {
                            return '永久';
                        }
                        var time = new Date(res.vip_expire * 1000);
                        var y = time.getFullYear();
                        var m = time.getMonth()+1;
                        var d = time.getDate();
                        return y + '-' + (m < 10?('0' + m):m) + '-' + (d < 10?('0' + d):d);
                    }}
                , {field: '', title: '盈利(USDT)', minWidth:110,align:"center",templet: function(res){
                        if(typeof(res.profit) == 'object') {
                            return 0;
                        }
                        return floorScale(res.profit,4);
                    }}
                , {field: '', title: '服务费(USDT)', minWidth:110,align:"center",templet: function(res){
                        if(typeof(res.service_charge_deducted) == 'object') {
                            return 0;
                        }
                        return floorScale(res.service_charge_deducted,4);
                    }}
                , {field: '', title: '余额(USDT)', minWidth:110,align:"center",templet: function(res){
                        if(typeof(res.local_balance) == 'object') {
                            return 0;
                        }
                        return floorScale(res.local_balance,4);
                    }}
                , {field:'', title:'收服务费', minWidth:110,align:"center",templet: function(res){
                        if(res.has_service_charge == 2) {
                            return '<tag class="layui-btn layui-bg-gray layui-btn-xs" lay-event="setService">否</tag>';
                        } else {
                            return '<tag class="layui-btn layui-btn-danger layui-btn-xs" lay-event="setService">' + floorScale(res.service_charge_rate * 100,4) + ' %</tag>';
                        }
                    }}
                , {field:'', title:'注册时间', minWidth:110,align:"center",templet: function(res){
                       return res.create_time.substring(0,10);
                    }}
                , {fixed: 'right', align: 'center', title: '操作', toolbar: '#list-bar', width: 150}
            ]]
            , page: true
            , id: 'list-table'
            , height: 'full-255'
            // ,limit:30
        });

        $("#search-btn").on("click",function (){
            table.reload('list-table', {
                page: {
                    curr: 1
                }
                ,where: {
                    key: {
                        has_service_charge : $('[name="has_service_charge"]').val(),
                        balance : $('[name="balance"]').val(),
                        username : $('[name="username"]').val()
                    }
                }
            }, 'data');
        });

        //头工具栏事件
        table.on('toolbar(list-box)', function (obj) {
            switch (obj.event) {
                case 'url':
                    window.location.href = "{:createUrl('editor')}";
                    break;
                case 'refresh':
                    window.location.reload();
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(list-box)', function (obj) {
            var data = obj.data;
            var username = '<font style="color:red;">' + data.username + '</font>';
            switch (obj.event) {
                case 'setVip':  // 设置 vip
                    layer.confirm('确定为客户[' + username + ']设置为永久 vip ？',{
                        title:'设置永久VIP'
                    }, function (index) {
                        $.post('{:createUrl("set_vip")}',{
                            id : data.id,
                            status : 1,
                        },function(result){
                            layer.close(index);
                            if(result.code){
                                layer.msg(result.msg,{ icon : 1 });
                                table.reload('list-table',{}, 'data');
                            }else{
                                layer.msg(result.msg,{ icon : 2 });
                            }
                        },"json");
                    });
                    break;
                case 'setService':  // 修改服务费
                    var string = '<form style="padding: 10px 40px 10px 20px;" class="layui-form" action="">';

                    string += '<div class="layui-form-item" style="margin: 0px;">';
                    string += '     <label class="layui-form-label">客户：</label>';
                    string += '     <div class="layui-form-label" style="text-align:left;min-width: 150px;">';
                    string += username;
                    string += '     </div>';
                    string += '</div>';


                    string += '<div class="layui-form-item" style="margin:0;">';
                    string += '     <label class="layui-form-label" style="padding-left: 0;" ">服务费率(%)：</label>';
                    string += '     <div class="layui-input-block">';
                    string += '         <input style="width:150px;" type="text" name="rate" value="' + floorScale(data.service_charge_rate * 100,4) + '" lay-reqtext="请输入百分比" lay-verify="required" placeholder="请输入百分比" autocomplete="off" class="layui-input">';
                    string += '     </div>';
                    string += '</div>';

                    string += '<div class="layui-form-item" style="margin:0">';
                    string += '     <label class="layui-form-label" style="padding-left: 0;" ">是否收取：</label>';
                    string += '     <div class="layui-input-block"  style="width:200px;">';
                    string += '    <input type="checkbox" name="status" title="收取"' + (data.has_service_charge == 1?' checked':'') + ' >';
                    string += '     </div>';
                    string += '</div>';



                    string += '<div style="padding-top: 20px;" class="layui-form-item">' +
                        '    <div class="layui-input-block">' +
                        '      <button class="layui-btn" lay-submit lay-filter="*" style="width:150px;">确认</button>' +
                        '    </div>' +
                        '  </div>';
                    string += '</form>';
                    index = layer.open({
                        title:'收取服务费',
                        type: 1,
                        skin: 'layui-layer-rim',
                        area: ['420px', '300px'],
                        content: string,
                        success: function(layero, index){
                            form.render("checkbox");
                        }
                    });

                    form.on('submit(*)', function(res){
                        res.field.id = data.id;
                        $.post('{:createUrl("set_service_charge")}',res.field,function (result) {
                            if(result.code){
                                layer.close(index);
                                layer.msg(result.msg,{ icon : 1 });
                                table.reload('list-table',{}, 'data');
                            }else{
                                layer.msg(result.msg,{ icon : 2 });
                            }
                        });

                        return false;
                    });

                    break;


                    layer.confirm('确定将[' + username + ']收费状态改为<font style="color:red;">' + (newStatus == 2?'否':'是') + '</font>？',{
                        title:'修改服务费状态'
                    }, function (index) {
                        $.post('{:createUrl("set_service_charge")}',{
                            id : data.id,
                            status : newStatus,
                        },function(result){
                            layer.close(index);
                            if(result.code){
                                layer.msg(result.msg,{ icon : 1 });
                                table.reload('list-table',{}, 'data');
                            }else{
                                layer.msg(result.msg,{ icon : 2 });
                            }
                        },"json");
                    });
                    break;
                case 'recharge':    // 充值处理
                    var string = '<form style="padding: 10px 40px 10px 20px;" class="layui-form" action="">';

                    string += '<div class="layui-form-item" style="margin: 0px;">';
                    string += '     <label class="layui-form-label">客户：</label>';
                    string += '     <div class="layui-form-label" style="text-align:left;min-width: 150px;">';
                    string += username;
                    string += '     </div>';
                    string += '</div>';


                    string += '<div class="layui-form-item">';
                    string += '     <label class="layui-form-label" style="padding-left: 0;" ">金额(USDT)：</label>';
                    string += '     <div class="layui-input-block">';
                    string += '         <input style="width:150px;" type="text" name="amount" value="" lay-reqtext="请输入数量" lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input">';
                    string += '     </div>';
                    string += '</div>';



                    string += '<div style="padding-top: 20px;" class="layui-form-item">' +
                        '    <div class="layui-input-block">' +
                        '      <button class="layui-btn" lay-submit lay-filter="*" style="width:150px;">确认</button>' +
                        '    </div>' +
                        '  </div>';
                    string += '</form>';
                    index = layer.open({
                        title:'充值',
                        type: 1,
                        skin: 'layui-layer-rim',
                        area: ['420px', '250px'],
                        content: string,
                        success: function(layero, index){
                            form.render("checkbox");
                        }
                    });

                    form.on('submit(*)', function(res){
                        res.field.id = data.id;
                        res.field.localAmount = data.local_amount;
                        $.post('{:createUrl("recharge")}',res.field,function (result) {
                            if(result.code){
                                layer.close(index);
                                layer.msg(result.msg,{ icon : 1 });
                                table.reload('list-table',{}, 'data');
                            }else{
                                layer.msg(result.msg,{ icon : 2 });
                            }
                        });

                        return false;
                    });
                    break;
                    case 'withdrawal':  // 提现处理
                        var string = '<form style="padding: 10px 40px 10px 20px;" class="layui-form" action="">';

                        string += '<div class="layui-form-item" style="margin: 0px;">';
                        string += '     <label class="layui-form-label">客户：</label>';
                        string += '     <div class="layui-form-label" style="text-align:left;min-width: 150px;">';
                        string += username;
                        string += '     </div>';
                        string += '</div>';

                        string += '<div class="layui-form-item" style="margin: 0px;">';
                        string += '      <label class="layui-form-label" style="padding-left: 0;">余额(USDT)：</label>';
                        string += '     <div class="layui-form-label" style="text-align:left;">';
                        string +=  floorScale(data.local_balance,4);
                        string += '     </div>';
                        string += '</div>';


                        string += '<div class="layui-form-item">';
                        string += '     <label class="layui-form-label" style="padding-left: 0;" ">金额(USDT)：</label>';
                        string += '     <div class="layui-input-block">';
                        string += '         <input style="width:150px;" type="text" name="amount" value="" lay-reqtext="请输入数量" lay-verify="required" placeholder="请输入数量" autocomplete="off" class="layui-input">';
                        string += '     </div>';
                        string += '</div>';



                        string += '<div style="padding-top: 20px;" class="layui-form-item">' +
                            '    <div class="layui-input-block">' +
                            '      <button class="layui-btn" lay-submit lay-filter="*" style="width:150px;">确认</button>' +
                            '    </div>' +
                            '  </div>';
                        string += '</form>';
                        index = layer.open({
                            title:'提现',
                            type: 1,
                            skin: 'layui-layer-rim',
                            area: ['420px', '300px'],
                            content: string,
                            success: function(layero, index){
                                form.render("checkbox");
                            }
                        });

                        form.on('submit(*)', function(res){
                            res.field.id = data.id;
                            res.field.localAmount = data.local_amount;
                            $.post('{:createUrl("withdrawal")}',res.field,function (result) {
                                if(result.code){
                                    layer.close(index);
                                    layer.msg(result.msg,{ icon : 1 });
                                    table.reload('list-table',{}, 'data');
                                }else{
                                    layer.msg(result.msg,{ icon : 2 });
                                }
                            });

                            return false;
                        });
                        break;
            }
            if (obj.event === 'del') {
                layer.confirm('你确定要删除吗？', function (index) {
                    $.get('{:createUrl("delete")}',{
                        id : data.id
                    },function(result){
                        layer.close(index);
                        if(result.code){
                            obj.del();
                        }else{
                            layer.msg(result.msg,{ icon : 2 });
                        }
                    },"json");
                });
            } else if (obj.event === 'edit') {
                window.location.href = '{:createUrl("editor")}?id='+data.id;
            }else if(obj.event == 'finance'){
                layer.open({
                    type: 2,
                    title: '金额管理',
                    shadeClose: true,
                    shade: 0.3,
                    area: ['60%', '58%'],
                    content: '{:createUrl("finance")}?id='+data.id
                });
            }else if(obj.event == 'log'){
                window.location.href = '{:createUrl("log")}?id='+data.id;
            }else if(obj.event == 'tags'){
                var string = '<form style="padding: 20px 20px 30px 20px;" class="layui-form" action="">';

                    string += '<div style="padding-top: 20px;" class="layui-form-item">\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>\n' +
                        '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
                        '    </div>\n' +
                        '  </div>';
                    string += '</form>';
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim',
                        area: ['420px', '240px'],
                        content: string,
                        success: function(layero, index){
                            form.render("checkbox");
                        }
                    });

                    form.on('submit(*)', function(res){
                        res.field.user_id = data.id;
                        $.post('{:createUrl("tags")}',res.field,function (res) {
                            console.log(res)
                        });
                        window.location.reload();
                        return false;
                    });
                }
            });


    });
</script>





